﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NoteSystem.Utils;

namespace NoteSystem
{
    public partial class User : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["User"] != null)
                {
                    InitUser();
                }
            }

        }

        public void InitUser()
        {
            #region 数据加载
            DataSet ds = DbHelper.Query("select * from users ");
            rptUser.DataSource = ds;
            rptUser.DataBind();
            #endregion

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            #region 全选删除
            int sign = 0;
            //遍历所有的数据行
            for (int i = 0; i < rptUser.Items.Count; i++)
            {
                //获取每一行的勾选框
                CheckBox cbx = rptUser.Items[i].FindControl("cbxItem") as CheckBox;
                int userId = Convert.ToInt32((rptUser.Items[i].FindControl("hfUserId") as HiddenField).Value);
                //判断勾选框是否勾选
                if (cbx.Checked == true)
                {
                    DataSet ds = DbHelper.Query("select manager from users where id=" + userId);
                    if (Convert.ToInt32(ds.Tables[0].Rows[0]["manager"]) == 0)
                    {
                        int flag = DbHelper.ExecuteSql("delete from users where id=" + userId);
                        if (flag > 0)
                        {
                            sign++;
                        }
                    }
                    else
                    {
                        Response.Write("<script>alert('禁止删管理员')</script>");
                    }

                }
            }
            if (sign > 0)
                Response.Write($"<script>alert('删除成功，共删除{sign}条')</script>");
            InitUser();
            #endregion


        }

        protected void rptUser_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            #region 审核权限
            //获取当前行的用户
            int userId = Convert.ToInt32((e.Item.FindControl("hfUserId") as HiddenField).Value);
            DataSet ds = DbHelper.Query("select * from users where id="+ userId);
            //获取当前用户审核状态
            int userType = Convert.ToInt32(ds.Tables[0].Rows[0]["type"]);
            //判断当前行的用户是否未审核
            //是:不处理
            //不是:删除隐藏
            if (userType != 0 )
            {
                e.Item.FindControl("LinkButton1").Visible = false;
            }
            #endregion
        }

        protected void rptUser_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            #region  审核
            int id = Convert.ToInt32((e.Item.FindControl("hfUserId") as HiddenField).Value);
            if (e.CommandName == "Audit")
            {
                string sql = string.Format("update users set type={0} where id={1}", 1, id);
                int flag = DbHelper.ExecuteSql(sql);
                if (flag > 0)
                {
                    Response.Write("<script>alert('审核成功')</script>");
                    InitUser();
                }
            }
            #endregion
        }
    }
}